#ifndef FKWORLDREFERENT_H
#define FKWORLDREFERENT_H

#include <QObject>

#include <QMap>
#include <QMatrix4x4>
#include <QImage>
#include <QPoint>

#include "fkglmodule_global.h"

class FKDrawModel;
class QOpenGLFramebufferObject;
//class FKActorsModel;

class FKGLMODULESHARED_EXPORT FKWorldReferent:public QObject{
    Q_OBJECT
public:
    FKWorldReferent(QObject* parent=0);
    ~FKWorldReferent();
    QOpenGLFramebufferObject* pickingBuffer(){return _pickingFBP;}
    void addModel(FKDrawModel* model);
    void resetModels();
    void moveCamera(QVector3D v);
    void turnCamera(QQuaternion q);

    QMatrix4x4 worldViewMatrix();
    FKDrawModel* getModel(QString modelName) const{return _drawModels.value(modelName,0);}
    const QList<FKDrawModel*> getModels() const{return _drawModels.values();}
public slots:
    void loadSceneRequest();
signals:
//    void worldAction(qint32 object, QString subject, QVariant value, qint32 emitter);
    void sceneLoadingComplete();
    void mouseClicked(QPoint pos, qint32 obj);
protected:
//    bool eventFilter(QObject* obj, QEvent* event);
    qint32 getObject(const QPoint &p);
private slots:
    void resize(QSize size);
    virtual void draw();
    void loadSceneCall();
    void areaClicked(QPoint pos);
private:
    virtual void loadScene();
    virtual void drawModelObjects(FKDrawModel* model, const QMatrix4x4& pMat, const QMatrix4x4& vMat);
    virtual void pickModelObjects(FKDrawModel* model, const QMatrix4x4& pMat, const QMatrix4x4& vMat);
    void applyResize();
    void calculateCamera();
    QMap<QString,FKDrawModel*> _drawModels;
    QQuaternion _cameraRotation;
    QVector3D _cameraPosition;
    bool _cameraCalculated;
    QMatrix4x4 _cameraViewMat;

    QMatrix4x4 _pMat;

    QOpenGLFramebufferObject* _pickingFBP;

    QSize _size;
    QSize _oldSize;

    bool _isLoading;
    bool _isLoaded;
    QImage _colorBuffer;
    QPoint _lastClicked;
//    float t;
};

#endif // FKWORLDREFERENT_H
